package de.homelabs.hlmodules.cms.template.domain;

import de.homelabs.hlmodules.cms.enums.HLErrorCode;

public class HLImportStrategyException extends Exception {

	// vars
	private static final long serialVersionUID = 1L;
	private HLErrorCode hlError = null;
	private Class<?> underlayingExceptionClass = null;
	private String underlayingExceptionMessage = "";

	/**
	 * Simple constructor with an error message
	 * 
	 * @param message
	 */
	public HLImportStrategyException(String message) {
		super(message);
	}

	/**
	 * Constructor with custom enum definition
	 * 
	 * @param error
	 */
	public HLImportStrategyException(HLErrorCode error) {
		this.hlError = error;
	}

	/**
	 * constructor which wraps the exception class and message from an
	 * underlaying exception
	 * 
	 * @param error
	 *            - {@link HLErrorCode}
	 * @param inheritExceptionClass
	 *            - the underlaying exception class
	 * @param inheritExceptionMessage
	 *            - the underlaying exception message
	 */
	public HLImportStrategyException(HLErrorCode error,
			Class<?> underlayingExceptionClass,
			String underlayingExceptionMessage) {
		this.hlError = error;
		this.underlayingExceptionClass = underlayingExceptionClass;
		this.underlayingExceptionMessage = underlayingExceptionMessage;
	}

	/****************
	 * functions *
	 ****************/
	/**
	 * returns the complete exceptions as text
	 * 
	 * @return String
	 */
	public String toFullErrorText() {
		StringBuffer buffer = new StringBuffer();
		// first test HLErrorCode
		if (hlError != null) {
			buffer.append("HLImportStrategyException [" + hlError.getCode()
					+ "]: " + hlError.getText() + "\n");
			// lets see if we have a wrapped error
			if (hlError.getId() == 0) {
				// we have an underlaying error
				buffer.append("Underlaying Exception [");
				if (underlayingExceptionClass != null)
					buffer.append(underlayingExceptionClass.getName());
				else
					buffer.append("unknown");
				buffer.append("]: ");
				if (underlayingExceptionMessage != null)
					buffer.append(underlayingExceptionMessage);
				else
					buffer.append("unknown");
				// terminate
				buffer.append("\n");
			}
		}

		// let see if we have some message
		buffer.append(this.getMessage());

		// return
		return buffer.toString();
	}

	/*******************
	 * getter / setter *
	 *******************/
	public HLErrorCode getHlError() {
		return hlError;
	}

	public void setHlError(HLErrorCode hlError) {
		this.hlError = hlError;
	}

	public Class<?> getUnderlayingExceptionClass() {
		return underlayingExceptionClass;
	}

	public void setUnderlayingExceptionClass(Class<?> underlayingExceptionClass) {
		this.underlayingExceptionClass = underlayingExceptionClass;
	}

	public String getUnderlayingExceptionMessage() {
		return underlayingExceptionMessage;
	}

	public void setUnderlayingExceptionMessage(
			String underlayingExceptionMessage) {
		this.underlayingExceptionMessage = underlayingExceptionMessage;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
}
